package com.avic.rong.activity.mine;

import android.content.Context;
import android.content.Intent;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.avic.rong.BaseActivity;
import com.avic.rong.R;
import com.avic.rong.api.SendCodeApi;
import com.avic.rong.api.mine.UpPhoneApi;
import com.avic.rong.utils.SharedPreferencesUtil;
import com.avic.rong.utils.ConstantsUtils;
import com.avic.rong.utils.RegexUtils;
import com.avic.rong.utils.StringUtils;
import com.avic.rong.utils.TimeCount;
import com.avic.rong.utils.ToastTools;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;

//账号设置-个人信息-修改手机号
public class UpPhoneActivity extends BaseActivity {

    @BindView(R.id.tv_phone)
    TextView tv_phone;
    @BindView(R.id.edittext_code)
    EditText edittext_code;
    @BindView(R.id.bt_sendcode)
    Button bt_sendcode;
    @BindView(R.id.edittext_newphone)
    EditText edittext_newphone;
    @BindView(R.id.edittext_newcode)
    EditText edittext_newcode;
    @BindView(R.id.bt_send_newcode)
    Button bt_send_newcode;
    @BindView(R.id.bt_ok_up)
    Button bt_ok_up;
    private SendCodeApi sendCodeApi;
    private String newPhone;
    private String login_token;
    private String mobile;


    @Override
    protected int getContentViewId() {
        return R.layout.activity_up_phone;
    }

    @Override
    protected void initView() {

        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }

        tvTitle.setText("修改手机号码");

    }

    @Override
    protected void addListener() {
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void getPhone(String phone) {
        this.mobile = phone;
        String substring = phone.substring(3, 7);
        String replace = phone.replace(substring, "****");
        tv_phone.setText(replace);
    }

    public static Intent queryIntent(Context context) {
        Intent intent = new Intent(context, UpPhoneActivity.class);
        return intent;
    }

    @OnClick({R.id.bt_sendcode, R.id.bt_send_newcode, R.id.bt_ok_up})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_sendcode:
                sendCode();
                break;
            case R.id.bt_send_newcode:
                sendNewCode();
                break;
            case R.id.bt_ok_up:
                upPhone();
                break;
        }
    }

    private void upPhone() {
        String code = edittext_code.getText().toString().trim();
        newPhone = edittext_newphone.getText().toString().trim();
        String newCode = edittext_newcode.getText().toString().trim();


        if (StringUtils.isEmpty(code)) {
            Toast toast = Toast.makeText(this, "原验证码不能为空", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
            return;
        }
        if (StringUtils.isEmpty(newCode)|| StringUtils.isEmpty(newCode)) {
            Toast toast = Toast.makeText(this, "新手机号码或新验证码不能为空", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
            return;
        }
        if (!RegexUtils.isMobileExact(newPhone)) {
            Toast toast = Toast.makeText(this, "新手机号码格式不正确", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
            return;
        }

        if (SharedPreferencesUtil.isLogin()) {
            login_token = SharedPreferencesUtil.getLoginToken();
        }


        UpPhoneApi upPhoneApi = new UpPhoneApi(this,upPhoneListener );
        upPhoneApi.setArgument(mobile,newCode, newPhone,code,login_token);
        HttpManager.getInstance().doHttpDeal(upPhoneApi);

    }


    HttpOnNextListener<BaseResultEntity> upPhoneListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity baseResultEntity) {
            if (baseResultEntity.getCode().equals(ConstantsUtils.STATUS_SUCCESS_0000)){
                ToastTools.showPublicToast("修改成功");
                EventBus.getDefault().post(newPhone);
                finish();
            }else {
                Toast toast = Toast.makeText(UpPhoneActivity.this, baseResultEntity.getMsg(), Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
            }
        }
    };


    private void sendNewCode() {
        sendCodeApi = new SendCodeApi(this, sendNewCodeListener);

        String phone = edittext_newphone.getText().toString().trim();
        sendCodeApi.setArguments(phone, 6, SharedPreferencesUtil.SIGN);
        HttpManager.getInstance().doHttpDeal(sendCodeApi);
    }


    HttpOnNextListener<BaseResultEntity> sendNewCodeListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity baseResultEntity) {
            if (baseResultEntity.getCode().equals(ConstantsUtils.STATUS_SUCCESS_0000)) {
                timeOut(bt_send_newcode);
            }else {
                Toast toast = Toast.makeText(UpPhoneActivity.this, baseResultEntity.getMsg(), Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
            }
        }
    };

    private void sendCode() {
        sendCodeApi = new SendCodeApi(this, sendCodeListener);
        sendCodeApi.setArguments(mobile, 3, SharedPreferencesUtil.SIGN);
        HttpManager.getInstance().doHttpDeal(sendCodeApi);
    }

    HttpOnNextListener<BaseResultEntity> sendCodeListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity baseResultEntity) {
            if (baseResultEntity.getCode().equals(ConstantsUtils.STATUS_SUCCESS_0000)) {
                timeOut(bt_sendcode);
            }
            Toast toast = Toast.makeText(UpPhoneActivity.this, baseResultEntity.getMsg(), Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
        }
    };

    private void timeOut(TextView tv) {
        TimeCount timeCount = new TimeCount(60000, 1000, tv,true);
        timeCount.start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

}
